package com.our.sys.project1.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.our.set.web.UserSession;
import com.our.sys.common.utils.AppUtil;
import com.our.sys.project1.RunConstants;
import com.our.sys.project1.entity.Person;
import com.our.sys.project1.entity.ReadyCode;
import com.our.sys.project1.entity.ReadySend;
import com.our.sys.project1.entity.RegAccount;
import com.our.sys.project1.entity.Toffices;
import com.our.sys.project1.service.PersonService;
import com.our.sys.project1.service.ReadyCodeService;
import com.our.sys.project1.service.RegAccountService;
import com.our.sys.project1.service.TofficesService;

@Controller
@RequestMapping("/reg_account")
public class RegAccountController {

    private static Logger logger = LoggerFactory.getLogger(RegAccountController.class);

    @Autowired
    private RegAccountService regAccountService;
    @Autowired
    private PersonService personService;
    @Autowired
    private TofficesService tofficesService;
    @Autowired
    private ReadyCodeService rcSercice;

    @RequestMapping(value = "/query_reg_account")
    public @ResponseBody
    Map<String, Object> queryReadyCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String name = request.getParameter("name");
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("1", RunConstants.READY_CODE);
        queryMap.put("2", name);
        List<RegAccount> data = new ArrayList<RegAccount>();
        List<ReadySend> rsData = regAccountService.queryList(queryMap);
        for (ReadySend rs : rsData) {
            Long tId = rs.getTofficesId();
            Long pId = rs.getPersonId();
            Long id = rs.getId();
            String readySendTime = rs.getReadySendTime();
            String readySendWho = rs.getReadySendWho();
            Person person = personService.queryById(pId);
            Toffices toffices = tofficesService.queryById(tId);
            List<ReadyCode> rcList = rcSercice.queryListBySendId(id);
            for (ReadyCode rc : rcList) {
                if (rc.getReadyCodeState().equals(RunConstants.READY_PRICE)) {
                    RegAccount ra = new RegAccount();
                    ra.setFactoryCode(rc.getFactoryCode());
                    ra.setManagerCode(rc.getManagerCode());
                    ra.setMaterialName(rc.getMaterialName());
                    ra.sethNumber(person.getNumber());
                    ra.setName(person.getName());
                    ra.settName(toffices == null ? "" : toffices.getName());
                    ra.setReadyCodeNumber(rc.getReadyCodeNumber());
                    ra.setReadyCodeId(rc.getId());
                    ra.setReadySendTime(readySendTime);
                    ra.setRegWho(readySendWho);
                    data.add(ra);
                }
            }
        }
        map = AppUtil.buildJSONDataResponse(data, (long) data.size());
        return map;
    }

    @RequestMapping(value = "/save_reg")
    public @ResponseBody
    Map<String, Object> saveReg(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String strs = request.getParameter("strs");
        UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        try {
            regAccountService.save(strs, u.getAccount());
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
}
